#include "Result.h"

using namespace util;

Result::Result(std::shared_ptr<Task> task, const ResultState state)
    : res_(nullptr)
    , task_(task)
    , state_(state)
    , sem_(0)
{
    assert(task != nullptr);
    task_->make_result(this);
}

void Result::put(void *res)
{
    if (ResultState::FAILURE == state_)
    {
        res_ = nullptr;
        return;
    }
    
    res_ = res;
    sem_.release();
}

void *Result::get()
{
    if (ResultState::FAILURE == state_)
    {
        res_ = nullptr;
        return res_;
    }

    sem_.acquire();
    return res_;
}
